/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.javacommon.base.BaseEntity;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Entity
@Table(name = "WEBSITE_TYPE")
public class WebsiteType extends BaseEntity implements java.io.Serializable{
	private static final long serialVersionUID = 5454155825314635342L;
	
	//alias
	public static final String TABLE_ALIAS = "网站类型";
	public static final String ALIAS_ID = "id";
	public static final String ALIAS_NAME = "类型名称";
	public static final String ALIAS_SORTING = "排序列";
	public static final String ALIAS_STATUS = "状态";
	public static final String ALIAS_PARENT_ID = "父类ID";
	
	//date formats
	

	//可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	//columns START
	
	private Long id;
	@NotBlank @Length(max=20)
	private String name;
	
	private String key;
	@NotNull 
	private Long sorting;
	@NotBlank @Length(max=1)
	private String status;
	
	private Long parentId;
	//columns END


	public WebsiteType(){
	}

	public WebsiteType(
		Long id
	){
		this.id = id;
	}

	

	public void setId(Long value) {
		this.id = value;
	}
	
	@Id 	@GeneratedValue(generator = "paymentableGenerator")       
	@GenericGenerator(name = "paymentableGenerator", strategy = "native") 
	@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getId() {
		return this.id;
	}
	
	@Column(name = "NAME", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public String getName() {
		return this.name;
	}
	
	public void setName(String value) {
		this.name = value;
	}
	
	@Column(name = "`KEY`")
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	@Column(name = "SORTING", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getSorting() {
		return this.sorting;
	}
	
	public void setSorting(Long value) {
		this.sorting = value;
	}
	
	@Column(name = "STATUS", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public String getStatus() {
		return this.status;
	}
	
	public void setStatus(String value) {
		this.status = value;
	}
	
	@Column(name = "PARENT_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
	public Long getParentId() {
		return this.parentId;
	}
	
	public void setParentId(Long value) {
		this.parentId = value;
	}
	
	
	private Set websiteTypes = new HashSet(0);
	public void setWebsiteTypes(Set<WebsiteType> websiteType){
		this.websiteTypes = websiteType;
	}
	
	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "websiteType")
	public Set<WebsiteType> getWebsiteTypes() {
		return websiteTypes;
	}
	
	private Set websites = new HashSet(0);
	public void setWebsites(Set<Website> website){
		this.websites = website;
	}
	
	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "websiteType")
	public Set<Website> getWebsites() {
		return websites;
	}
	
	private WebsiteType websiteType;
	public void setWebsiteType(WebsiteType websiteType){
		if(websiteType != null)
			websiteType.getName();
		this.websiteType = websiteType;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = "PARENT_ID",nullable = false, insertable = false, updatable = false) 
	})
	public WebsiteType getWebsiteType() {
		return websiteType;
	}

	public String toString() {
		return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
			.append("Id",getId())
			.append("Name",getName())
			.append("Sorting",getSorting())
			.append("Status",getStatus())
			.append("ParentId",getParentId())
			.toString();
	}
	
	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.toHashCode();
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof WebsiteType == false) return false;
		if(this == obj) return true;
		WebsiteType other = (WebsiteType)obj;
		return new EqualsBuilder()
			.append(getId(),other.getId())
			.isEquals();
	}
	@Transient
	@Override
	public String getTarget() {
		// TODO Auto-generated method stub
		return "网站类型";
	}
	@Transient
	@Override
	public Long getTargetId() {
		// TODO Auto-generated method stub
		return getId();
	}
	@Transient
	@Override
	public String getTargetName() {
		// TODO Auto-generated method stub
		return getName();
	}
}

